package com.my.study.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.my.study.sync.SyncService;
import com.my.study.sync.SyncUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author yidujun
 * @version 1.0.0
 * @date 2023/7/26 14:02
 */
@EnableAsync
@Configuration
public class ThreadPoolConfig {
    
    @Bean("commonPool")
    public ThreadPoolExecutor commonPool() {
        ThreadFactory threadFactory = ThreadFactoryBuilder.create()
                .setNamePrefix("test-")
                .setDaemon(true).build();
                
        return new ThreadPoolExecutor(
                5, 
                5,
                60, 
                TimeUnit.SECONDS, 
                new ArrayBlockingQueue<>(20), 
                threadFactory,
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
    
    @Bean
    public SyncService syncService() {
        return new SyncService();
    }
    
    @Bean
    public SyncUtils syncUtils() {
        return new SyncUtils();
    }
    
}
